package com.mall.common.plugin.storage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ListObjectsRequest;
import com.aliyun.openservices.oss.model.OSSObjectSummary;
import com.aliyun.openservices.oss.model.ObjectListing;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.mall.common.bean.FileInfo;

@Component(value="ossPlugin")
public class OssPlugin extends StoragePlugin {

	public void upload(String path, File file, String contentType) {
		JSONObject config = getConfig();
		if(config == null){
			return;
		}
			
		String ACCESS_ID = config.getString("ACCESS_ID");
		String ACCESS_KEY = config.getString("ACCESS_KEY");
		String BUCKET = config.getString("BUCKET");
		
		FileInputStream fileinputstream = null;
		try {
			fileinputstream = new FileInputStream(file);
			OSSClient ossclient = new OSSClient(ACCESS_ID, ACCESS_KEY);
			ObjectMetadata objectmetadata = new ObjectMetadata();
			objectmetadata.setContentType(contentType);
			objectmetadata.setContentLength(file.length());
			ossclient.putObject(BUCKET, StringUtils.removeStart(path, "/"), fileinputstream, objectmetadata);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(fileinputstream);
		}
	}
	
	public String getUrl(String path) {
		JSONObject config = getConfig();
		if (config != null) {
			String url = config.getString("URL");
			return new StringBuilder(url).append(path).toString();
		}
		return null;
	}
	
	public String getNativeUrl(String path){
		JSONObject config = getConfig();
		if (config != null) {
			String url = config.getString("URL");
			return new StringBuilder(url).append(path).toString();
		}
		return null;
	}
	
	public List<FileInfo> browser(String path) {
		
		JSONObject config = getConfig();
		if (config == null) {
			return null;
		}
		String ACCESS_ID = config.getString("ACCESS_ID");
		String ACCESS_KEY = config.getString("ACCESS_KEY");
		String BUCKET = config.getString("BUCKET");
		String url = config.getString("URL");
		List<FileInfo> fileList = new ArrayList<FileInfo>();
		try {
			OSSClient ossclient = new OSSClient(ACCESS_ID, ACCESS_KEY);
			ListObjectsRequest listobjectsrequest = new ListObjectsRequest(BUCKET);
			listobjectsrequest.setPrefix(StringUtils.removeStart(path, "/"));
			listobjectsrequest.setDelimiter("/");
			ObjectListing objectlisting = ossclient.listObjects(listobjectsrequest);
			for (Iterator<String> iterator = objectlisting.getCommonPrefixes().iterator(); 
				iterator.hasNext(); ) {
				String s4 = iterator.next();
				FileInfo fileinfo = new FileInfo();
				fileinfo.setName(StringUtils.substringAfterLast(StringUtils.removeEnd(s4, "/"), "/"));
				fileinfo.setUrl(new StringBuilder(url).append("/").append(s4).toString());
				fileinfo.setStorePath(new StringBuilder(url).append("/").append(s4).toString());
				fileinfo.setIsDirectory(true);
				fileinfo.setSize(0L);
				fileList.add(fileinfo);
			}

			for (Iterator<OSSObjectSummary> iterator1 = objectlisting.getObjectSummaries().iterator(); iterator1.hasNext();){
				OSSObjectSummary ossobjectsummary = iterator1.next();
				if (!ossobjectsummary.getKey().endsWith("/")) {
					FileInfo fileinfo1 = new FileInfo();
					fileinfo1.setName(StringUtils.substringAfterLast(ossobjectsummary.getKey(), "/"));
					fileinfo1.setUrl(new StringBuilder(String.valueOf(url)).append("/").append(ossobjectsummary.getKey()).toString());
					fileinfo1.setStorePath(this.getId() + "://" + ossobjectsummary.getKey());
					fileinfo1.setIsDirectory(false);
					fileinfo1.setSize(ossobjectsummary.getSize());
					fileinfo1.setLastModified(ossobjectsummary.getLastModified());
					fileList.add(fileinfo1);
				}
			}

		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return fileList;
	}
	
	@Override
	public String getType() {
		return "storage";
	}

	public String getName(){
		return "阿里云存储";
	}

	public String getVersion(){
		return "1.0.0";
	}

	public String getAuthor(){
		return "zhenglong";
	}

	@Override
	public void perform(Object... params) {
		
	}

	@Override
	public void register() {
		
	}

}
